package com.ahzx.report.service.impl;


import com.ahzx.report.mapper.CreditLoanReportMapper;
import com.ahzx.report.mapper.ReportMapper;
import com.ahzx.report.request.CompanyDetailQuery;
import com.ahzx.report.request.ReportQuery;
import com.ahzx.report.response.*;
import com.ahzx.report.service.ReportService;
import com.ahzx.utils.Builder;
import com.ahzx.utils.DecimalUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 报表统计业务实现层
 *
 * @Author syg
 * @Date 2021/4/25 17:23
 * @Desc To be or not to be,that is a question.
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private CreditLoanReportMapper creditLoanReportMapper;

    @Override
    public List<CompanyRegisterResponse> selectCompanyRegisterReportList(ReportQuery reportQuery) {
        setEndTime(reportQuery);
        AtomicInteger index = new AtomicInteger(1);
        List<CompanyRegisterResponse> companyRegisterResponses = reportMapper.selectCompanyRegisterReportList(reportQuery);
        companyRegisterResponses.forEach(c -> c.setIndex(index.getAndAdd(1)));
        return companyRegisterResponses;
    }

    @Override
    public List<CompanyRegisterDetailResponse> selectCompanyRegisterDetailReportList(CompanyDetailQuery companyDetailQuery) {
        if (StringUtils.isNotEmpty(companyDetailQuery.getEndTime())) {
            companyDetailQuery.setEndTime(companyDetailQuery.getEndTime() + "235959");
        }
        return reportMapper.selectCompanyRegisterDetailReportList(companyDetailQuery);
    }

    @Override
    public List<PlatformDemandResponse> selectPlatformDemandReportList(ReportQuery reportQuery) {
        setEndTime(reportQuery);
        return reportMapper.selectPlatformDemandReportList(reportQuery);
    }


    @Override
    public List<FinanceResponse> selectFinanceReportList(ReportQuery reportQuery) {
        setEndTime(reportQuery);
        return reportMapper.selectFinanceReportList(reportQuery);
    }

    private void setEndTime(ReportQuery reportQuery) {
        if (StringUtils.isNotEmpty(reportQuery.getEndTime())) {
            reportQuery.setEndTime(reportQuery.getEndTime() + "235959");
        }
    }

    @Override
    public Map<String, CreditLoanResponse> selectCreditLoanReportList(ReportQuery reportQuery) {
        setEndTime(reportQuery);
        CreditLoanResponse creditLoanResponse = Builder.of(CreditLoanResponse::new)
                .with(CreditLoanResponse::setCompanyRegisterNumber, Optional.ofNullable(creditLoanReportMapper.selectCountCompanyRegisterNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setFinanceOrgNumber, Optional.ofNullable(creditLoanReportMapper.selectCountFinanceOrgNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setFinanceProductNumber, Optional.ofNullable(creditLoanReportMapper.selectCountFinanceProductNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setDemandCompanyNumber, Optional.ofNullable(creditLoanReportMapper.selectCountDemandCompanyNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setDemandAmt, Optional.ofNullable(DecimalUtils.scale(DecimalUtils.divide(creditLoanReportMapper.selectCountDemandAmt(reportQuery), DecimalUtils.toBigDecimal("100000000")), 2))
                        .orElse(DecimalUtils.toBigDecimal("0")))
                .with(CreditLoanResponse::setDemandNumber, Optional.ofNullable(creditLoanReportMapper.selectCountDemandNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setCreditCompanyNumber, Optional.ofNullable(creditLoanReportMapper.selectCountCreditCompanyNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setCreditAmt, Optional.ofNullable(DecimalUtils.scale(DecimalUtils.divide(creditLoanReportMapper.selectCountCreditAmt(reportQuery), DecimalUtils.toBigDecimal("100000000")), 2))
                                .orElse(DecimalUtils.toBigDecimal("0")))
                .with(CreditLoanResponse::setCreditNumber, Optional.ofNullable(creditLoanReportMapper.selectCountCreditNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setLoanCompanyNumber, Optional.ofNullable(creditLoanReportMapper.selectCountLoanCompanyNumber(reportQuery)).orElse(0))
                .with(CreditLoanResponse::setLoanAmt, Optional.ofNullable(DecimalUtils.scale(DecimalUtils.divide(creditLoanReportMapper.selectCountLoanAmt(reportQuery), DecimalUtils.toBigDecimal("100000000")), 2))
                        .orElse(DecimalUtils.toBigDecimal("0")))
                .with(CreditLoanResponse::setLoanNumber, Optional.ofNullable(creditLoanReportMapper.selectCountLoanNumber(reportQuery)).orElse(0))
                .build();
        Map<String, CreditLoanResponse> map = new HashMap<>();
        map.put("month" , creditLoanResponse);
        return map;
    }
}
